<template>
  <div class="workstage_start">
    <el-dialog
      title="开工登记"
      width="30%"
      top="10%"
      :visible.sync="dialogTableVisible"
      v-dialogDrag
      :close-on-click-modal="false"
      customClass="mes_dialog"
      @closed="closedDialog"
      v-loading="isLoading">
     <div class="batch_list_start">
       <p>是否对以下批次进行开工?</p>
       <div>
         <span class="batch_number" v-for="(item, index) in turnoverNumberList" :key="index">{{item}}</span>
       </div>
     </div>
      <span slot="footer" class="dialog-footer mes_btn">
          <el-button type="primary" @click="workstageStart">
            开工
          </el-button>
          <el-button @click="dialogTableVisible=false">
            取消
          </el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
export default {
  name: 'WorkStageStart',
  data() {
    return {
      dialogTableVisible: false,
      workstageList: [], // 勾选列表
      saveStatus: true, // 保存禁用
      turnoverNumberList: [], // 周转编号列表
      isLoading: false, // 是否加载中
    }
  },
  methods: {
    /**
     * 开启弹框
     * @param list
     */
    openDialog(list) {
      this.workstageList = list
      this.dialogTableVisible = true
      this.dataClean()
      this.getTimeTips()
    },
    // 数据去重
    dataClean() {
      let list = this.workstageList.map((item) => {
        return item.turnoverNumber
      })
      this.turnoverNumberList = list.filter((element, index, self) => {
        return self.indexOf(element) === index
      })
    },
    // 开工前间隔时间提示请求
    getTimeTips() {
      this.isLoading = true
      let list = this.workstageList.map((item) => item.id)
      this.$store.dispatch('saveData', {
        method: 'post',
        url: 'a/turnoverworkstage/turnoverWorkstage/checkQTime',
        data: {
          turnoverWorkstageIds: list
        },
        noCommit: true
      }).then((res) => {
        if (res.data && res.data.length) {
          this.turnoverNumberList = res.data
        }
      }).finally(() => {
        this.isLoading = false
      })
    },
    // 开工发送
    workstageStart() {
      if (this.saveStatus) {
        this.saveStatus = false
        let list = this.workstageList.map((item) => item.id)
        this.$store.dispatch('saveData', {
          method: 'post',
          url: 'a/turnoverworkstage/turnoverWorkstage/startworkreg',
          data: {
            turnoverWorkstageIds: list
          },
          noCommit: true
        }).then(() => {
          this.$emit('submitSuccess')
          this.$message({
            type: 'success',
            message: '开工成功',
            duration: 1500,
            center: true
          })
        }).finally(() => {
          this.dialogTableVisible = false
        }).catch(() => {
          this.saveStatus = true
        })
      }
    },
    // 关闭弹窗
    closedDialog() {
      this.saveStatus = true
    }
  }
}
</script>

<style lang="less">
  .batch_list_start{
    p{
      margin-bottom: 10px;
    }
    .batch_number{
      display: inline-block;
      padding-right: 20px;
    }
  }
</style>
